package com.ZengQi.event.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class VisitorsMainView extends JFrame implements ActionListener {

    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JButton loginBtn=new JButton("登录");
    JButton companyBtn=new JButton("企业信息");
    JLabel searchLabel=new JLabel("请输入查找的题目");
    JButton searchBtn=new JButton("找题");
    JTextField searchTxt=new JTextField();
    JPanel northPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JLabel loginLabel=new JLabel("您当前为游客状态若需要答题与评论请先登录");
    public VisitorsMainView(){
        super("做题帮");
        Container contentPane=getContentPane();
        layoutNorth(contentPane);

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        loginLabel.setFont(centerFont);
        loginBtn.setFont(centerFont);
        companyBtn.setFont(new Font("楷体",Font.PLAIN,50));
        searchBtn.setFont(centerFont);
        searchTxt.setPreferredSize(new Dimension(200, 30));
        searchLabel.setFont(centerFont);

        centerPanel.add(companyBtn);
        centerPanel.add(searchBtn);
        centerPanel.add(searchTxt);
        centerPanel.add(searchLabel);
        contentPane.add(centerPanel,BorderLayout.CENTER);

        loginBtn.addActionListener(this);
        companyBtn.addActionListener(this);
        searchBtn.addActionListener(this);

        layoutCenter();
        //设置图标
        URL resource = VisitorsMainView.class.getClassLoader().getResource("bang.png");
        Image image = new ImageIcon(resource).getImage();
        setIconImage(image);
        //主界面最大化
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(true);//使界面可视化
        //居中
        setLocationRelativeTo(null);
        //关闭时退出程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //大小可以改变
        setResizable(true);
    }

    private void layoutCenter() {
        //找题功能放在水平中心
        Spring childWidth = Spring.sum(Spring.sum(Spring.sum(Spring.width(searchLabel), Spring.width(searchTxt)), Spring.constant(20)),Spring.width(searchBtn));
        int offsetX = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, searchLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, searchLabel, 40, SpringLayout.NORTH, centerPanel);

        springLayout.putConstraint(SpringLayout.WEST, searchTxt, 20, SpringLayout.EAST, searchLabel);
        springLayout.putConstraint(SpringLayout.NORTH, searchTxt, 0, SpringLayout.NORTH, searchLabel);

        springLayout.putConstraint(SpringLayout.WEST, searchBtn, 10, SpringLayout.EAST, searchTxt);
        springLayout.putConstraint(SpringLayout.NORTH, searchBtn, 0, SpringLayout.NORTH, searchTxt);

        springLayout.putConstraint(SpringLayout.EAST, companyBtn, -10, SpringLayout.WEST, searchLabel);
        springLayout.putConstraint(SpringLayout.NORTH, companyBtn, 0, SpringLayout.NORTH, searchLabel);


    }

    private void layoutNorth(Container contentPane){
        northPanel.add(loginLabel);
        northPanel.add(loginBtn);
        contentPane.add(northPanel,BorderLayout.NORTH);
    }





    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton= (JButton) e.getSource();
        String text =jButton.getText();
        if("登录".equals(text)){
            new LoginView();
            dispose();
        }else if("找题".equals(text)){
            if(searchTxt.getText()==null||"".equals(searchTxt.getText().trim())){
                JOptionPane.showMessageDialog(this, "题目不能为空！");
                return;
            }
            new VisitorSearchView(this);
           dispose();
        }else if("企业信息".equals(text)){
            new CompanyView();
        }

    }



    public JTextField getSearchTxt() {
        return searchTxt;
    }
}
